// --------------------------------------------------------------------------------------------------------------------
// <copyright file="LocalizationService.cs" company="WestEngine">
//   
// </copyright>
// <summary>
//   Provides information about localization
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Web7.Container.Services.Impl
{
    using System.Web;
    using System.Web.Mvc;
    using Caching;
    using Localization;
    using Repositories;
    using Core;

    /// <summary>
    /// Provides information about localization
    /// </summary>
    public class LocalizationService : ILocalizationService
    {
        #region Fields

        private readonly ILocalizationRepository _lsrRepository;
        private readonly ILocalizedStringManager _localizedStringManager;
        private readonly ICacheManager _cacheManager;

        #endregion

        public LocalizationService(
            ILocalizationRepository repository,
            ILocalizedStringManager localizedStringManager,
            ICacheManager cacheManager){

            _lsrRepository = repository;
            _localizedStringManager = localizedStringManager;
            _cacheManager = cacheManager;
        }

        /// <summary>
        /// Gets Context.
        /// </summary>
        protected Web7Context Context
        {
            get
            {
                return DependencyResolver.Current.GetService<Web7Context>();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="resourceKey"></param>
        /// <returns></returns>
        public virtual string GetLocalizedString(string resourceKey)
        {
            if (!string.IsNullOrEmpty(resourceKey))
            {
                string preferenceCulture = Context.Site.CurrentLanguageCulture ?? LocalizationSettings.DefaultCulture;
                if (Context.Site.SelectPreferenceCulture)
                {
                    var cookie = HttpContext.Current.Request.Cookies[LocalizationSettings.PreferenceCulture];
                    if (cookie != null && !string.IsNullOrEmpty(cookie.Value))
                    {
                        preferenceCulture = cookie.Value;
                    }
                }
                return _localizedStringManager.GetLocalizedString("", resourceKey, preferenceCulture);
            }
            return resourceKey;
        }
    }
}
